//
//  BaseDemoVC.m
//  JAGatherAdDemo
//
//  Created by Yc on 2022/4/24.
//

#import "BaseDemoVC.h"
#import "Header.h"
#import "PlacementVC.h"
#import "NavigationVC.h"

@interface BaseDemoVC ()<PlacementEvent,UITextFieldDelegate>{
    NSString *_placement;
}
@property (nonatomic, strong) UILabel *adLabel;
@property (nonatomic, strong) UIButton *loadAdBtn;
@property (nonatomic, strong) UIButton *showAdBtn;
@property (nonatomic, strong) UIButton *loadShowAdBtn;
@property (nonatomic, strong) UIButton *nativeListAdBtn;
@property (nonatomic, strong) UIButton *moreBtn;
@property (nonatomic, weak) id<PlacementEvent> delegate;
@property (nonatomic, strong) UITextField *adTF;
@end

@implementation BaseDemoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    _delegate = self;
    [self setupUI];
}

- (void)setupUI {
    CGFloat pad = 15;
    CGFloat btnW = ([UIScreen mainScreen].bounds.size.width - pad*3)/2;
    CGFloat btnH = 50;
    self.moreBtn = [self setupBtn:@"更多" Frame:CGRectMake(([UIScreen mainScreen].bounds.size.width-pad-80), 10, 80, btnH)];
    self.loadAdBtn = [self setupBtn:@"loadAd" Frame:CGRectMake(pad, CGRectGetMaxY(self.adLabel.frame)+pad, btnW, btnH)];
    self.showAdBtn = [self setupBtn:@"show" Frame:CGRectMake(CGRectGetMaxX(self.loadAdBtn.frame)+pad, CGRectGetMaxY(self.adLabel.frame)+pad, btnW, btnH)];
    self.loadShowAdBtn = [self setupBtn:@"loadAndShow" Frame:CGRectMake(pad, CGRectGetMaxY(self.showAdBtn.frame)+pad, btnW*2+pad, btnH)];
    self.nativeListAdBtn = [self setupBtn:@"NativeListAd" Frame:CGRectMake(pad, CGRectGetMaxY(self.loadShowAdBtn.frame)+pad, btnW*2+pad, btnH)];
    self.nativeListAdBtn.hidden = YES;
    
    [self.view addSubview:self.adLabel];
    [self.view addSubview:self.adTF];
    [self.view addSubview:self.moreBtn];
    [self.view addSubview:self.loadAdBtn];
    [self.view addSubview:self.showAdBtn];
    [self.view addSubview:self.loadShowAdBtn];
    [self.view addSubview:self.nativeListAdBtn];
    
    [self.loadAdBtn addTarget:self action:@selector(loadAdAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.showAdBtn addTarget:self action:@selector(showAdAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.loadShowAdBtn addTarget:self action:@selector(loadAndShowAdAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.nativeListAdBtn addTarget:self action:@selector(loadNativeListAdAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.moreBtn addTarget:self action:@selector(loadMoreAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.adTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}

- (void)viewWillLayoutSubviews {
    self.adLabel.frame = CGRectMake(15, 10, 70, 50);
    self.adTF.frame = CGRectMake(CGRectGetMaxX(self.adLabel.frame), 10, ([UIScreen mainScreen].bounds.size.width - CGRectGetMaxX(self.adLabel.frame) - 15*2-80), 50);
    
    CGFloat pad = 15;
    CGFloat btnW = ([UIScreen mainScreen].bounds.size.width - pad*3)/2;
    CGFloat btnH = 50;
    
    self.moreBtn.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width-pad-80), 10, 80, btnH);
    self.loadAdBtn.frame = CGRectMake(pad, CGRectGetMaxY(self.adLabel.frame)+pad, btnW, btnH);
    self.showAdBtn.frame = CGRectMake(CGRectGetMaxX(self.loadAdBtn.frame)+pad, CGRectGetMaxY(self.adLabel.frame)+pad, btnW, btnH);
    self.loadShowAdBtn.frame = CGRectMake(pad, CGRectGetMaxY(self.showAdBtn.frame)+pad, btnW*2+pad, btnH);
    self.nativeListAdBtn.frame = CGRectMake(pad, CGRectGetMaxY(self.loadShowAdBtn.frame)+pad, btnW*2+pad, btnH);
}

- (UIButton *)setupBtn:(NSString *)name Frame:(CGRect)frame {
    UIButton *btn = [[UIButton alloc] initWithFrame:frame];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    [btn setTitle:name forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor colorWithRed:225/255.0 green:233/255.0 blue:239/255.0 alpha:1]];
    ViewRadius(btn,3);
    return btn;
}

- (void)loadAdAction {
    [self.adTF endEditing:YES];
}

- (void)showAdAction {
    [self.adTF endEditing:YES];
}

- (void)loadAndShowAdAction {
    [self.adTF endEditing:YES];
}

- (void)loadNativeListAdAction {
    
}

- (void)loadMoreAction {
    [self.adTF endEditing:YES];
    PlacementVC *vc = [[PlacementVC alloc] init];
    vc.dataList = self.placementList.copy;
    __weak typeof(self) weakSelf = self;
    [vc setTapCell:^(NSString * _Nonnull placementId) {
        weakSelf.adTF.text = placementId;
        if (self->_delegate && [self->_delegate respondsToSelector:@selector(reselectPlacementAction)]) {
            [self->_delegate reselectPlacementAction];
        }
    }];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)onlyLoadAndShowType:(BOOL)type {
    self.loadAdBtn.hidden = type;
    self.showAdBtn.hidden = type;
    self.loadShowAdBtn.hidden = !type;
    
    if (type) {
        self.loadShowAdBtn.frame = CGRectMake(15, CGRectGetMinY(self.loadAdBtn.frame), [UIScreen mainScreen].bounds.size.width - 15*2, 50);
    }
}

- (CGFloat)getNativeListAdButtonMaxY {
    return CGRectGetMaxY(self.nativeListAdBtn.frame);
}

- (void)showNativeListAdButton {
    self.nativeListAdBtn.hidden = NO;
}

- (void)writePlacementDataOfType:(DataType)type {
    if (self.placementList==nil) {return;}
    if (![self.placementList containsObject:self.adTF.text]&&self.adTF.text.length>0) {
        [self.placementList addObject:self.adTF.text];
        [[NSUserDefaults standardUserDefaults] setObject:self.placementList.copy forKey:[self getPlacementKey:type]];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

- (NSArray *)readPlacementDataOfType:(DataType)type {
    return [[NSUserDefaults standardUserDefaults] objectForKey:[self getPlacementKey:type]];
}

- (NSString *)getPlacementKey:(DataType)type {
    return [NSString stringWithFormat:@"PlacementKey%zd",type];
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidChange:(UITextField *)textField {
    
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    _placement = textField.text;
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (![_placement isEqualToString:textField.text]) {
        if (_delegate && [_delegate respondsToSelector:@selector(reselectPlacementAction)]) {
            [_delegate reselectPlacementAction];
        }
    }
}

- (void)setupPlacement:(NSString *)placement {
    _placement = placement;
    self.adTF.text = placement;
}

- (NSString *)getPlacement {
    return self.adTF.text;
}

- (void)tapAdLabel:(UITapGestureRecognizer *)tap {
    if (self.adTF.text.length==0) {
        return;
    }
    
    NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *fileDirectory = [docPath stringByAppendingPathComponent:@"JAGatgerSDK"];
    NSString *filePath = [fileDirectory stringByAppendingPathComponent:self.adTF.text];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        BOOL res = [fileManager removeItemAtPath:filePath error:nil];
        if (res) {
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:[NSString stringWithFormat:@"JAG_PolicyUpdateTimeKey%@",self.adTF.text]];
            TOAST(@"广告位策略缓存已清除");
        }
    }
}

#pragma mark - Lazy
- (UILabel *)adLabel {
    if (!_adLabel) {
        _adLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 70, 50)];
        _adLabel.font = [UIFont systemFontOfSize:16];
        _adLabel.textColor = [UIColor blackColor];
        _adLabel.text = @"广告位：";
    }
    return _adLabel;
}

- (UITextField *)adTF {
    if (!_adTF) {
        _adTF = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.adLabel.frame), 10, ([UIScreen mainScreen].bounds.size.width - CGRectGetMaxX(self.adLabel.frame) - 15*2-80), 50)];
        _adTF.font = [UIFont systemFontOfSize:16];
        _adTF.textColor = [UIColor blackColor];
        _adTF.backgroundColor = [UIColor whiteColor];
        _adTF.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 50)];
        _adTF.borderStyle = UITextBorderStyleRoundedRect;
        _adTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _adTF.delegate = self;
    }
    return _adTF;
}

@end
